package org.maxgroundstation.groundstation.view;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.TitledBorder;

import org.maxgroundstation.groundstation.ControllerInputPoller;
import org.maxgroundstation.groundstation.GroundStation;
import org.maxgroundstation.groundstation.GroundStationEvent;
import org.maxgroundstation.groundstation.GroundStationListener;

/**
 * A panel for input selection controls.
 * @author Scott Davis
 * 
 * Copyright (C) 2010  Scott Davis
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
@SuppressWarnings("serial")
public class InputSelectionPanel extends JPanel implements GroundStationListener {

    // Data members.
    private GroundStation gs;
    private JRadioButton joystickButton;
    private JRadioButton keyboardMouseButton;
    
    /**
     * Constructor
     * @param gs the ground station.
     */
    public InputSelectionPanel(GroundStation gs) {
        super();
        
        // Initialize data members
        this.gs = gs;
        
        // Set layout
        setLayout(new GridLayout(2, 1));
        
        // Set border.
        setBorder(new TitledBorder("Input Device Selection"));
        
        // Create the device button group.
        ButtonGroup deviceButtonGroup = new ButtonGroup();

        // Create joystick radio button.
        joystickButton = new JRadioButton("Joystick - Not Detected");
        deviceButtonGroup.add(joystickButton);
        joystickButton.setEnabled(false);
        joystickButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                updateUseJoystick();
            }
        });
        joystickButton.setToolTipText("Select to control rover with the joystick.");
        add(joystickButton);

        // Create keyboard & mouse radio button.
        keyboardMouseButton = new JRadioButton("Keyboard & Mouse");
        deviceButtonGroup.add(keyboardMouseButton);
        keyboardMouseButton.setSelected(true);
        keyboardMouseButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                updateUseJoystick();
            }
        });
        keyboardMouseButton.setToolTipText("Select to control rover with the keyboard and mouse.");
        add(keyboardMouseButton);
        
        // Update the input buttons.
        updateInputButtons();
        
        // Register as ground station listener.
        gs.addGroundStationListener(this);
    }
    
    @Override
    public void groundStationUpdate(GroundStationEvent event) {
        
        // Update controllers radio buttons if joystick detected or not.
        if (GroundStationEvent.CONTROLLERS_DETECTED.equals(event.getType())) updateInputButtons(); 
    }
    
    /**
     * Updates if the joystick controller should be used.
     */
    private void updateUseJoystick() {
        gs.getInputPoller().setUseJoystick(joystickButton.isSelected());
    }
    
    /**
     * Update the input buttons based on detected input devices.
     */
    public void updateInputButtons() {
        ControllerInputPoller inputPoller = gs.getInputPoller();
        if (inputPoller.hasJoystick()) {
            joystickButton.setEnabled(true);
            joystickButton.setText("Joystick - Detected");
            if (keyboardMouseButton.isSelected()) {
                joystickButton.setSelected(true);
                updateUseJoystick();
            }
        }
        else {
            joystickButton.setEnabled(false);
            joystickButton.setText("Joystick - Not Detected");
            if (joystickButton.isSelected()) {
                keyboardMouseButton.setSelected(true);
                updateUseJoystick();
            }
        }
    }
}